package icu.luoxi.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @author Cao Jiwen
 * @date 2025/3/31 16:30
 * @description SHA256加密工具类
 */
public class SHA256Utils {

    /**
     * 盐值
     */
    private static final String SALT = "2233jfk**3nkjnj??";

    public static String encrypt(String input) {
        try {
            // 获取SHA-256消息摘要实例
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            // 将输入字符串和盐值组合起来
            String combinedInput = input + SALT;
            // 计算组合字符串的SHA-256哈希值
            byte[] messageDigest = md.digest(combinedInput.getBytes(StandardCharsets.UTF_8));
            // 将哈希值转换为Base64编码的字符串
            return Base64.getEncoder().encodeToString(messageDigest);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("SHA-256算法未找到", e);
        }
    }
}
